<?php
class ProductController extends Vnf_Controller_Action {
    function init() {

        $this->view->maximizeWin = true; // maximize window after popup
        $this->view->winWidth = 640; // default width of window
        $this->view->winHeight = 480; // default height of window
        parent::init();

        $this->setLayout('admin');

        $this->view->translate = new Zend_Translate('tmx', BASE_DIR . '/translate/admin.tmx', 'en');

        $this->view->currentController = $this->_request->getControllerName();
        $this->view->objName = "Category"; // class name of Model
        $objName = $this->view->objName;
        $this->view->objModel = $obj = new $objName();

        $this->view->pk = $obj->getPrimaryKey();
    }

    public function indexAction() {
//        Zend_Layout::getLayout('content');
    }

    public function listAction() {
        $this->view->catID = $catID = $this->_request->getParam('cid', 0);
        $all = $this->_request->getParam('all', 0);
        
        
        
        $where = null;
        if ($catID > 0) {
            $where = "category_id in  ($catID)";
        }
        
        //$objCategory = new Category();
        //$allCategory = $objCategory->getCacheOfCategory();
        
        //$this->view->thisCat = $allCategory[$catID];
        //$objCategory->fetchRow($where);
        // get child page
        if ($all == 1) {
            $allCategory = $this->view->allCategory;
            $currentCat = @$allCategory[$catID];
            
            $pageWithSubPage = $currentCat['children'];
            // join current page
            //$pageWithSubPage[] = $currentPage;//['category_id'];
            $pageWithSubPage[] = $catID;//['category_id'];
            // parse to string
            $strPageWithSubPage = implode(',', $pageWithSubPage);
            
            $where = "category_id in  ($strPageWithSubPage)";
        }
        
        $objItem = new Product();
        
        $this->view->totalRecords = $totalRecords = $objItem->getTotalRecords("$where");

            $currentPaging = (int) $this->_request->getParam('page', 1);
            $currentPaging--;
            $numberPageView = 20;
            $recordsPerPage = (int) $this->_request->getParam('records', 20);
            $recordsPerPage = $recordsPerPage > 0? $recordsPerPage:20;
            $totalPages = ceil($totalRecords/$recordsPerPage);
            $this->view->pages = $totalPages;

            //echo $currentPage;
            $link =  BASE_URL . "/product/list/?id=$catID&all=$all";
            
            $tagID = "showbycat";
            $startRecord = $recordsPerPage * $currentPaging;
            //$order = 'CommentID DESC';
            $class = "paging";

            $paging = new Paging();
            $this->view->paging = $paging->getPaging($currentPaging + 1, $totalPages, $link, $numberPageView, $tagID, $class,'false');

            $pk = $objItem->getPrimaryKey();
            $items = $objItem->getRecords($where, "$pk DESC", $recordsPerPage, $recordsPerPage * $currentPaging);


            $this->view->start = $start = $recordsPerPage * $currentPaging;
            
            $this->view->allItems = $items;
            
    }
    
    public function addAction() {
    }
}